package com.example.careycai.notepad;

import android.app.Activity;

import android.content.Intent;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class mulu extends Activity {
    private Button addinfo;
    private MyDatabaseHelper dbHelper;

    private List<Information_entity>information_entityList=new ArrayList<Information_entity>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initInfomation_entity();
        Information_entityAdapter adapter=new Information_entityAdapter(mulu.this,R.layout.mulu_item,information_entityList);
        ListView listView=(ListView)findViewById(R.id.listView);
        listView.setAdapter(adapter);

        dbHelper=new MyDatabaseHelper(this,"Notepad.db",null,1);



        addinfo=(Button)findViewById(R.id.creat);
        addinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mulu.this,Addinfo.class);
                startActivity(intent);
                finish();
            }
        });


        addinfo=(Button)findViewById(R.id.creat);
        addinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();
                Intent intent=new Intent(mulu.this,Addinfo.class);
                startActivity(intent);

            }
        });


        //listView.setOnLongClickListener(new View.OnLongClickListener() {




        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                Information_entity information_entity=information_entityList.get(position);
                Dialog dialog=new AlertDialog.Builder(mulu.this)
                        .setTitle("删除信息")
                        .setMessage("您确定要删除该条信息吗？")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int which) {
                                        Toast.makeText(mulu.this,"删除记事本",Toast.LENGTH_LONG).show();
                                        // finish(); 为了演示效果，不进行真实退出，用土司代替
                                    }
                                })

                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface arg0, int arg1) {
                                    }
                                }).create();
                dialog.show();

                //Toast.makeText(mulu.this,information_entity.getTitle(),Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void initInfomation_entity() {
        Information_entity title1=new Information_entity(1,"标题1","place","content","time");
        information_entityList.add(title1);

        Information_entity title2=new Information_entity(2,"标题2","place","content","time");
        information_entityList.add(title2);

        Information_entity title3=new Information_entity(3,"标题3","place","content","time");
        information_entityList.add(title3);

        Information_entity title4=new Information_entity(4,"标题4","place","content","time");
        information_entityList.add(title4);

        Information_entity title5=new Information_entity(5,"标题5","place","content","time");
        information_entityList.add(title5);

        Information_entity title6=new Information_entity(6,"标题6","place","content","time");
        information_entityList.add(title6);

        Information_entity title7=new Information_entity(7,"标题7","place","content","time");
        information_entityList.add(title7);

        Information_entity title8=new Information_entity(8,"标题8","place","content","time");
        information_entityList.add(title8);

        Information_entity title9=new Information_entity(9,"标题9","place","content","time");
        information_entityList.add(title9);

        Information_entity title10=new Information_entity(10,"标题10","place","content","time");
        information_entityList.add(title10);

        Information_entity title11=new Information_entity(11,"标题11","place","content","time");
        information_entityList.add(title11);

        Information_entity title12=new Information_entity(12,"标题12","place","content","time");
        information_entityList.add(title12);

        Information_entity title13=new Information_entity(13,"标题13","place","content","time");
        information_entityList.add(title13);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_mulu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
